/**
 * Copyright (c) 2010 BullionXpress.com,
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * @author Heidi Rakels
 * @author BullionXpress.com
 */
package com.bullionxpress.client.view;

import com.javadocking.dockable.Dockable;
import com.javadocking.dockable.DockableState;
import com.javadocking.dockable.action.DefaultDockableStateAction;
import com.javadocking.event.DockingEvent;
import com.javadocking.event.DockingListener;
import java.awt.event.ActionEvent;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.Action;
import javax.swing.JMenuItem;

/**
     * A listener that listens when menu items with dockables are selected and deselected.
     * It also listens when dockables are closed or docked.
     */
    public class DockableMediator implements ItemListener, DockingListener {

        private Dockable dockable;
        private Action closeAction;
        private Action restoreAction;
        private JMenuItem dockableMenuItem;

        public DockableMediator(Dockable dockable, JMenuItem dockableMenuItem) {

            this.dockable = dockable;
            this.dockableMenuItem = dockableMenuItem;
            closeAction = new DefaultDockableStateAction(dockable, DockableState.CLOSED);
            restoreAction = new DefaultDockableStateAction(dockable, DockableState.NORMAL);

        }

        public void itemStateChanged(ItemEvent itemEvent) {

            dockable.removeDockingListener(this);
            if (itemEvent.getStateChange() == ItemEvent.DESELECTED) {
                // Close the dockable.
                closeAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Close"));
            } else {
                // Restore the dockable.
                restoreAction.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "Restore"));
            }
            dockable.addDockingListener(this);

        }

        public void dockingChanged(DockingEvent dockingEvent) {
            if (dockingEvent.getDestinationDock() != null) {
                dockableMenuItem.removeItemListener(this);
                dockableMenuItem.setSelected(true);
                dockableMenuItem.addItemListener(this);
            } else {
                dockableMenuItem.removeItemListener(this);
                dockableMenuItem.setSelected(false);
                dockableMenuItem.addItemListener(this);
            }
        }

        public void dockingWillChange(DockingEvent dockingEvent) {
        }
    }
